package physics;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Timer;

public class World implements ActionListener{
	public static final int ACTION_RATE = 30; // in actions per second. 
	
	private ActorList actors = new ActorList();
	private Timer t;
	
	
	public World(){
		t = new Timer(1000/ACTION_RATE, this);
	}
	
	/**
	 * Add the given obstacle to the world.
	 * @param 	obstacle
	 * 				The obstacle to add.
	 */
	public void addObstacle(Obstacle obstacle){
		obstacle.setWorld(this);
		actors.add(obstacle);
	}
	
	/**
	 * Get the obstacles covering the given coordinate.
	 * @param 	coordinate
	 * 				The coordinate to check.
	 * @return
	 * 			List of obstacles on the given coordinate.
	 */
	public List<Obstacle> getObstacle(Coordinate coordinate){
		List<Obstacle> result = new ArrayList<Obstacle>();
		for (Obstacle o : getObstacles())
			if (o.getShape().contains(coordinate.toPoint()))
				result.add(o);
		return result;
	}
	
	/**
	 * @return	A list of the obstacles in this world.
	 */
	public List<Obstacle> getObstacles(){
		return new ArrayList<Obstacle>(actors.getActors(Obstacle.class));
	}
	
	/**
	 * Let's the world change over time.
	 */
	public void start(){
		t.start();
	}
	
	/**
	 * Stop the world from changing over Time.
	 */
	public void stop(){
		t.stop();
	}

	private void updateObstacles() {
		for (Obstacle o : this.getObstacles()){
			o.act();
		}
	}

	public List<Vector> getActingVectors(Coordinate location) {
		List<Vector> result = new ArrayList<Vector>();
		
		// TODO: get all the vectors on the given location.
		
		return result;
	}

	/**
	 * Checks if the given obstacle is part of this world.
	 * @param 	obstacle
	 * 				The obstacle to check.
	 * @return	True if the obstacle is part of this world.
	 */
	public boolean hasObstacle(Obstacle obstacle) {
		return getObstacles().contains(obstacle);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		this.updateObstacles();
	}

	
}
